const app = getApp()
const util = require('../../utils/util.js')
Page({
  data: {
    TabCur: 0,
    type:1,
    scrollLeft:0,
    host:app.globalData.host,
    modalName:'',
    topic_id:'',
    nofollow_parent_id:'',
    login:'',
    tabList:[
      {
        image:'/images/tuwen.png',
        name:'图文',
        count:0,
      },
      {
        image:'/images/wenda.png',
        name:'问答',
        count:0,
      },
      {
        image:'/images/fenxiang.png',
        name:'分享',
        count:0,
      },
  ]
  },
  // BackPage:function(){
  //   wx.switchTab({
  //     url: '/pages/baike/baike',
  //   })
  // },
   /**
   * 关注
   * @param {*} e 
   */
  follow:function(e){
    var data = e.target.dataset, id =data.id;
    var that = this;
    util.request('follow/add', {type:2,topic_id:id}, function (data) {
      console.log(data);
      if(data.code == 1){
        that.setData({
          'topic.follow':1,
        })
        wx.showToast({
          title: '关注成功',
          icon: 'success',
          duration: 1500
        })
      }else{
        util.fail(data.msg,that);
      }
    });
  },
   /**
   * 取消关注
   * @param {*} e 
   */
  nofollow:function(e){
    var that = this;
    util.request('follow/delete', {type:2,topic_id:that.data.topic.id}, function (data) {
      console.log(data);
      if(data.code == 1){
        that.setData({
          'topic.follow':0,
        })
        that.hideModal();
        wx.showToast({
          title: '操作成功',
          icon: 'success',
          duration: 1500
        })
      }else{
        util.fail(data.msg,that);
      }
    });
  },

  showModal_1:function(e){
    this.setData({
      modalName: 'nofollow',
    })
  },

  tabSelect(e) {
    this.setData({
      TabCur: e.currentTarget.dataset.id,
      type:e.currentTarget.dataset.id+1,
      scrollLeft: (e.currentTarget.dataset.id-1)*60
    })
    this.getMsgList(1,'');
  },

  fabu:function(){
    this.setData({
      modalName: 'bottomModal',
    })
  },
  hideModal:function(){
    this.setData({
      modalName: '',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad:function(e){
    var that = this,topic_id = e.topic_id;
    that.setData({
      topic_id:topic_id,
    })
    // 主题信息
    util.request('topic/detail', {topic_id:topic_id}, function (data) {
      console.log(data);
      if(data.code == 1){
        var tabList = that.data.tabList;
        tabList[0].count = data.data.topic.article_num,
        tabList[1].count = data.data.topic.question_num,
        tabList[2].count = data.data.topic.share_num,
        that.setData({
          topic:data.data.topic,
          tabList:tabList
        })
      }else{
        util.fail(data.msg,that);
      }
    });

    //内容列表
    this.getMsgList(1,'');

  },

  ViewImage(e) {
    var array = this.data.pageList[e.currentTarget.dataset.index].image_array,arr =[];
    for(var i = 0;i<array.length;i++){
      arr[i] = this.data.host+array[i];
    }
    wx.previewImage({
      urls: arr,
      current: e.currentTarget.dataset.url
    });
  },

  showModalParent:function(e){
    this.setData({
      modalName: 'modalParent',
      nofollow_parent_id:e.target.dataset.parent_id
    })
  },

  changeFollow:function(parent_id,child_id,status){
    var pageList = this.data.pageList;
    if(status == 1){
      for(var i =0 ;i< pageList.length;i++){
        if(pageList[i].parent_id == parent_id && pageList[i].child_id == child_id ){
          pageList[i].follow = status;
        }
      }
    }
    if(status == 0){
      for(var i =0 ;i< pageList.length;i++){
        if(pageList[i].parent_id == parent_id ){
          pageList[i].follow = status;
        }
      }
    }
    this.setData({
      pageList:pageList
    });
  },
  /**
   * 关注
   * @param {*} e 
   */
  follow_parent:function(e){
    var data = e.target.dataset, parent_id =data.parent_id,child_id = data.child_id,index = data.index;
    var that = this;
    var row = 'pageList['+index+'].follow';
    util.request('follow/add', {type:1,parent_id:parent_id,child_id:child_id}, function (data) {
      console.log(data);
      if(data.code == 1){
       that.changeFollow(parent_id,child_id,1);
        wx.showToast({
          title: '关注成功',
          icon: 'success',
          duration: 1500
        })
      }else{
        util.fail(data.msg,that);
      }
    });
  },

    /**
   * 取消关注
   * @param {*} e 
   */
  nofollow_parent:function(e){
    var parent_id = this.data.nofollow_parent_id;
    var that = this;
    util.request('follow/delete', {type:1,parent_id:parent_id}, function (data) {
      console.log(data);
      if(data.code == 1){
        that.setData({
          nofollow_parent_id:''
        })
        that.changeFollow(parent_id,'',0);
        that.hideModal();
        wx.showToast({
          title: '操作成功',
          icon: 'success',
          duration: 1500
        })
      }else{
        that.setData({
          nofollow_parent_id:''
        })
        util.fail(data.msg,that);
      }
    });
  },

    /**
   * 收藏/取消收藏
   * @param {*} e 
   */
  collect:function(e){
    var index = e.currentTarget.dataset.index,data = this.data.pageList[index],that = this;
    var collect = data.collect;
    util.request('collect/save', {related_id:data['id']}, function (data) {
      if(data.code == 1){
        var row = 'pageList['+index+'].collect';
        if(collect.is_collect > 0){
          that.setData({
              [row]:{
                count:collect.count-1,
                is_collect:0,
              }
          })
        }else{
          that.setData({
            [row]:{
              count:collect.count+1,
              is_collect:1,
            }
        })
        }
      }else{
        util.fail(data.msg,that);
      }
    });
  },



  /**
   * 点赞/取消点赞
   * @param {*} e 
   */
  thumb:function(e){
    var index = e.currentTarget.dataset.index,data = this.data.pageList[index],that = this;
    var thumb = data.thumb;
    util.request('thumb/save', {type:3,related_id:data['id']}, function (data) {
      if(data.code == 1){
        var row = 'pageList['+index+'].thumb';
        if(thumb.is_thumb > 0){
          that.setData({
              [row]:{
                count:thumb.count-1,
                is_thumb:0,
              }
          })
        }else{
          that.setData({
            [row]:{
              count:thumb.count+1,
              is_thumb:1,
            }
        })
        }
      }else{
        util.fail(data.msg,that);
      }
    });
  },


  onShow:function(){
    this.hideModal();
    this.setData({
      login:app.globalData.login,
    })
  },

  onPullDownRefresh: function () {
    if (!this.loading) {
      wx.showNavigationBarLoading();
      this.getMsgList(1,'top');
    }
  },

  onReachBottom: function () {
    var that = this;
    if (!this.data.loading && this.data.page < this.data.pages) {
      that.setData({
        isHideLoadMore: false,
        page: this.data.page+1
      });
      console.log(that.data.page);
      this.getMsgList(that.data.page, 'bottom');
    }
  },

  getMsgList: function(pageNo,pos) {
    var url = 'article/index',param = {type:this.data.type,topic_id:this.data.topic_id};
    if(this.data.type == 2){
      param['is_reply'] = 1;
    }
    param.pageNo = pageNo;
    console.log(param);
    util.pageList(this,url,param, pos);
  },
})
